package top.chj.day18.genericity.yingyong2;

import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

/**
 * 定制排序：先按年龄排序，年龄相同按照姓名排序
 */
public class User1 {
    private int id;
    private String name;
    private int age;

    public User1() {
    }

    public User1(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User1{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        User1[] user1 = new User1[3];
        user1[0] = new User1(1,"张三",21);
        user1[1] = new User1(2,"李四",20);
        user1[2] = new User1(3,"王五",20);
        Arrays.sort(user1, new Comparator<User1>() {
            @Override
            public int compare(User1 o1, User1 o2) {
                if(o1.getAge()!= o2.getAge()){
                    return o1.getAge()-o2.getAge();
                }else {
                    Collator collator = Collator.getInstance(Locale.CHINA);
                    return collator.compare(o1.getName(),o2.getName());
                }
            }
        });
        System.out.println(Arrays.toString(user1));
    }
}

